package net.javalib.gvmax.post.server.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.io.IOUtils;


public class MiscUtil {

	public static String normalizeNumber(String number) {
		if (number == null)
			return null;
		number = number.replace(" ","");
		number = number.replace("+", "");
		number = number.replace("-", "");
		number = number.replace("(", "");
		number = number.replace(")", "");
		return number;
	}
	
	public static String getEmailText(MimeMessage message) throws IOException {
		try {
			String text = null;
			if (message.getContent() instanceof ByteArrayInputStream) {
				ByteArrayInputStream in = (ByteArrayInputStream) message.getContent();
				text = IOUtils.toString(in);
				in.close();
			} else if (message.getContent() instanceof MimeMultipart) {
				MimeMultipart mm = (MimeMultipart) message.getContent();
				text = mm.getBodyPart(0).getContent().toString();
			} else {
				text = message.getContent().toString();
			}
			return text;
		} catch (Exception e) {
			throw new IOException(e);
		}
	}
	
	
}
